package com.spring.cloub.study.event;

import org.springframework.context.ApplicationEvent;
import org.springframework.context.ApplicationListener;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

/**
 * 上下文驱动以及事件监听
 * created by YDW
 */
public class SpringEventStudy {


    public static void main(String[] args) {
        AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
        //注册监听器
        context.addApplicationListener(new ApplicationListener<MyApplicationEvent>() {
            @Override
            //监听器得到消息
            public void onApplicationEvent(MyApplicationEvent applicationEvent) {
                System.out.println(applicationEvent.getSource());
            }
        });

        context.refresh();
        //发布事件
        context.publishEvent(new MyApplicationEvent("监听接受到消息"+111));
        context.publishEvent(new MyApplicationEvent("监听接受到消息"+"哈哈哈哈"));
        context.publishEvent(new MyApplicationEvent("监听接受到消息"+"啦啦啦啦啦"));

    }


    private static class MyApplicationEvent extends ApplicationEvent {
        public MyApplicationEvent(Object source) {
            super(source);
        }
    }
}
